home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / tspa3155.zip / TSUNTB.INT < prev    next >
Text File  |  1992-09-27  |  7KB  |  194 lines

  1. {$B-,D-,F-,I+,N-,R-,S+,V+}
  2.  
  3. (*
  4. Timo Salmi UNiT B
  5. A Turbo Pascal unit for (a) bit (of) manipulation
  6. All rights reserved 22-Jul-89,
  7. Updated 26-Jul-89, 19-Aug-89, 18-Oct-89, 17-Jul-90, 4-Jan-91, 27-Oct-91
  8.         23-Aug-92, 27-Sep-92
  9.  
  10. Here are some power functions, bit manipulation and base conversions.
  11. There is nothing really novel about them, just that I have needed them
  12. myself, and thought to make some available to [a/be]muse the general public.
  13. Some of the functions are, however, hopefully both smaller faster than the
  14. corresponding routines in average Turbo Pascal guides, or have features
  15. that are normally lacking.
  16.  
  17. This unit may be used and distributed freely for PRIVATE, NON-COMMERCIAL,
  18. NON-INSTITUTIONAL purposes, provided it is not changed in any way. For
  19. ANY other usage, such as use in a business enterprise or at a university,
  20. contact the author for registration.
  21.  
  22. The units are under development. Comments and contacts are solicited. If
  23. you have any questions, please do not hesitate to use electronic mail for
  24. communication.
  25. InterNet address: ts@uwasa.fi         (preferred)
  26. Bitnet address:   SALMI@FINFUN
  27.  
  28. The author shall not be liable to the user for any direct, indirect or
  29. consequential loss arising from the use of, or inability to use, any unit,
  30. program or file howsoever caused. No warranty is given that the units and
  31. programs will work under all circumstances.
  32.  
  33. Timo Salmi
  34. Professor of Accounting and Business Finance
  35. Faculty of Accounting & Industrial Management; University of Vaasa
  36. P.O. BOX 297, SF-65101 Vaasa, Finland
  37.  
  38. The following changes were made 4-Jan-91:
  39.  POWERGFN function has been rewritten
  40.  DECBINFN function has been omitted
  41.  DECHEXFN function has been omitted
  42.  
  43. The following routines were added 27-Oct-91:
  44.  BTEWRDFN
  45.  WRDLNGFN
  46.  HIWORDFN
  47.  LOWORDFN
  48.  
  49. The following routine was added 22-Aug-92:
  50.  HEXLNGFN
  51.  
  52. The following routines were added 27-Sep-92:
  53.  BHEXFN
  54.  BBINFN
  55.  
  56. *)
  57.  
  58. unit TSUNTB;
  59.  
  60. (* ======================================================================= *)
  61.                           interface
  62. (* ======================================================================= *)
  63.  
  64. uses Dos;
  65.  
  66. (* =======================================================================
  67.                        Timer routines
  68.    ======================================================================= *)
  69.  
  70. (*
  71. If one wants to test and compare speeds of various procedures and functions
  72. one needs a procedure to give the elapsed time. TIMERFN does that. It is
  73. very simple. It just gives the seconds elapsed since midnight to the apparent
  74. precision of a hundredth of a second.
  75. *)
  76.  
  77. (* Time (seconds) elapsed since midnight *)
  78. function TIMERFN : real;
  79.  
  80. (* The timer is actually updated 18.2 times per seconds by the 8284
  81.    oscillator at 1193180 Hetz frequency. These updates are called ticks.
  82.    Ticks are calculated from midnight, and can be used as an alternative
  83.    timing. *)
  84. function TICKSFN : longint;
  85.  
  86. (*
  87. Pascal lacks a power function and therefore one has to build it oneself.
  88. The simplest way to calculate a power function is Exp(exponent*Ln(number)),
  89. which many guides suggest. It is unsatisfactory, however, from the point
  90. of view that powers of zero or negative numbers cause an error. The
  91. POWERGFN does not have this hitch, but is, of course, slightly slower.
  92. Invalid operations such as -0.55 to -3.5 are detected and running halted.
  93. In special cases a power function can be made very fast. TWOTOFN is such a
  94. function raising two to a power.
  95. *)
  96.  
  97. (* =======================================================================
  98.            Mathematical routines for raising to powers
  99.    ======================================================================= *)
  100.  
  101. (* Raise a positive number to a power the traditional way *)
  102. function POWERFN (number, exponent : real) : real;
  103.  
  104. (* Raise any number to a power, the generalized power function.
  105.    Invalid expressions and overflows are trapped properly *)
  106. function POWERGFN (number, exponent : real) : real;
  107.  
  108. (* Raise a longint to a power, fast; Do not use negative exponents *)
  109. function POWERLFN (number, exponent : longint) : longint;
  110.  
  111. (* Raise two to a power, that is 2^exponent, very fast *)
  112. function TWOTOFN (exponent : word) : word;
  113.  
  114. (* Raise sixteen to a power, that is 16^exponent, very fast *)
  115. function R16TOFN (exponent : word) : word;
  116.  
  117. (* =======================================================================
  118.              Mathematical number base conversion routines
  119.    ======================================================================= *)
  120.  
  121. (*
  122. Base conversion is a quite commonly occurring task in programming. It
  123. involves a similar problem to the one explained in discussing the
  124. routines for raising a number to a power: The routines can be made
  125. general, or they can be made fast. Here are some of the routines.
  126. *)
  127.  
  128. (* Convert a number from any base to any base (2-36),
  129.    The result may not execeed the range of a longint *)
  130. function CONVBFN (number : string; frombase, tobase : byte) : string;
  131.  
  132. (* ---- decimal to hexadecimal ---- *)
  133.  
  134. (* Convert a decimal byte to a hexadecimal string *)
  135. function BHEXFN (decimal : byte) : string;
  136.  
  137. (* Convert a decimal word to a hexadecimal string *)
  138. function HEXFN (decimal : word) : string;
  139.  
  140. (* Convert a decimal longint to a hexadecimal string *)
  141. function LHEXFN (decimal : longint) : string;
  142.  
  143. (* ---- decimal to binary ---- *)
  144.  
  145. (* Convert a decimal word to a binary string *)
  146. function BINFN (decimal : word) : string;
  147.  
  148. (* Convert a decimal longint to a binary string *)
  149. function LBINFN (decimal : longint) : string;
  150.  
  151. (* Convert a decimal byte to a binary string *)
  152. function BBINFN (decimal : byte) : string;
  153.  
  154. (* ---- hexadecimal to decimal ---- *)
  155.  
  156. (* Convert a hexadecimal string fast to a decimal word *)
  157. function HEXDECFN (hexadecimal : string) : word;
  158.  
  159. (* Convert a hexadecimal string to a longint, largest 7FFFFFFF *)
  160. function HEXLNGFN (hexadecimal : string) : longint;
  161.  
  162. (* ---- binary to decimal ---- *)
  163.  
  164. (* Convert a binary string fast to a decimal word *)
  165. function BINDECFN (binary : string) : word;
  166.  
  167. (* =======================================================================
  168.        Bit, byte, word, and longint datatype manipulation routines
  169.    ======================================================================= *)
  170.  
  171. (*
  172. The next function is specialized, but occasionally very useful. As is
  173. known a word is made up of 16 bits numbered from 0 to 15. The following
  174. function establishes whether a particular bit is on or off in a word
  175. *)
  176. (* Is an individual bit on in a word, bits are numbered from 0-15 as usual *)
  177. function BITONFN (status : word; bit : byte) : boolean;
  178.  
  179. (* Combines two bytes into a single word.
  180.    This is the inverse of the inbuilt Hi and Lo byte functions *)
  181. function BTEWRDFN (high, low : byte) : word;
  182.  
  183. (* Combines two words into a longint *)
  184. function WRDLNGFN (high, low : word) : longint;
  185.  
  186. (* Returns the high-order word of the longint argument
  187.    Similar to Hi, but returns a word instead of a byte *)
  188. function HIWORDFN (x : longint) : word;
  189.  
  190. (* Returns the low-order word of the longint argument
  191.    Similar to Lo, but returns a word instead of a byte *)
  192. function LOWORDFN (x : longint) : word;
  193.  
  194.